papermanagermodule.controller('resultstatisModal', ['$scope', 'resultStatisFactory', '$rootScope', 'sysUserInfo', 'RequestTrainService', 'Comm', 'MsgCon', 'Msg', '$sce', '$stateParams', '$state', 'resultStatisFactory', function ($scope, resultStatisFactory, $rootScope, sysUserInfo, RTS, Comm, MsgCon, Msg, $sce, $stateParams, $state, resultStatisFactory) {

    $scope.token = sysUserInfo.getuserinfotoken();
    $scope.headurl = $rootScope.requestUrl;
    $scope.urlexamlopen = "?id=2&scoreId=";

    /****************************************************人员选择器************************************************/
    //人员选择器的定位
    $scope.mousePosition = function (ev) {
        if (!ev) {
            ev = window.event;
        }

        if (ev.type != "click") {
            return;
        }
        var width = 0;
        var height = 0;
        if (ev.pageX || ev.pageY) {//firefox、chrome等浏览器
            if (document.documentElement.clientWidth - ev.pageX < 450) {
                width = ev.pageX - (450 - (document.documentElement.clientWidth - ev.pageX)) - 20;
            } else {
                width = ev.pageX;
            }
            if (document.documentElement.clientHeight - ev.pageY < 480) {
                height = ev.pageY - (480 - (document.documentElement.clientHeight - ev.pageY)) - 20;
            } else {
                height = ev.pageY;
            }

            return { "left": width + "px", "top": height + "px", "position": "fixed" };
        }

        return {// IE浏览器
            "left": ev.clientX + document.body.scrollLeft - document.body.clientLeft,
            "top": ev.clientY + document.body.scrollTop - document.body.clientTop
        };
    }

    // 人员选择器                
    $scope.personData = {
        menu: [//进入人员选择器的菜单
                    {"id": "Org" }, { "id": "Role" }, { "id": "Group"}],
        ApiOpen: false,
        windowModal: 1,
        userMore: false, //不要全选，和已选
        style: "",
        ViceuserOrgList: [],
        ViceuserRoleList: [],
        ViceuserGroupList: [],
        searchType: [],  //初始化教师查询
        //searchTexts: ,  // 初始化教师查询
        DB: "0",
        DBdata: [],
        userOrgList: [],
        userGroupList: [],
        userRoleList: []
    }

    //打开人员悬着器
    $scope.openPerson = function (event) {
        $scope.personData.style = $scope.mousePosition(event);
        $scope.personData.ApiOpen = true;
    }
    //删除选中的部门
    $scope.delOrg = function (obj) {
        $scope.personData.userOrgList.forEach(function (data, index) {
            if (data.organization_ID == obj.organization_ID) {
                $scope.personData.userOrgList.splice(index, 1);
            }
        });
        $scope.reloadList();
    }
    //删除选中的用户组
    $scope.delGroup = function (obj) {
        $scope.personData.userGroupList.forEach(function (data, index) {
            if (data.userGroup_ID == obj.userGroup_ID) {
                $scope.personData.userGroupList.splice(index, 1);
            }
        });
        $scope.reloadList();
    }
    //删除选中的角色
    $scope.delRole = function (obj) {
        $scope.personData.userRoleList.forEach(function (data, index) {
            if (data.roles_ID == obj.roles_ID) {
                $scope.personData.userRoleList.splice(index, 1);
            }
        });
        $scope.reloadList();
    }
    //关闭事件
    $scope.$watch('personData.ApiOpen', function (newValue, oldValue, $scope) {
        $scope.reloadList();
    }, true);

    //重新查询
    $scope.reloadList = function () {
        //组织架构
        if ($scope.personData.userOrgList.length > 0) {
            $scope.personData.userOrgList.forEach(function (data, index) {
                $scope.params.organizationID = ($scope.params.organizationID.length > 0 ? ',' : "") + data.organization_ID;
            });
        } else {
            $scope.params.organizationID = "";
        }
        //用户组
        if ($scope.personData.userGroupList.length > 0) {
            $scope.personData.userGroupList.forEach(function (data, index) {
                $scope.params.userGroupID = ($scope.params.userGroupID.length > 0 ? ',' : "") + data.userGroup_ID;
            });
        } else {
            $scope.params.userGroupID = "";
        }
        //角色
        if ($scope.personData.userRoleList.length > 0) {
            $scope.personData.userRoleList.forEach(function (data, index) {
                $scope.params.userRoleID = ($scope.params.userRoleID.length > 0 ? ',' : "") + data.roles_ID;
            });
        } else {
            $scope.params.userRoleID = "";
        }
        $scope.GetList();
    }
    //关闭窗口
    $scope.closeWindow = function () {
        resultStatisFactory.deactivate();
    }
    $scope.isloading1 = false;
    //初始化参数
    $scope.params = {
        paperid: $scope.resultid,
        orgid: sysUserInfo.getuserinfo().organization_ID,
        organizationID: "",
        userGroupID: "",
        userRoleID: "",
        orderby: "desc",
        searchText: "",
        correct: "3",
        single: "2", //全部  1 单人最高传2
        sort: "1",
        pageIndex: "1",
        pageSize: "10",
        export: "1"

    };

    /*分页*/
    $scope.paginationConf = {
        currentPage: 1,
        totalItems: 0,
        itemsPerPage: 10,
        perPageOptions: [10, 20],
        onChange: function () {
            if ((parseInt($scope.params.pageIndex) != parseInt($scope.paginationConf.currentPage)) || (parseInt($scope.params.pageSize) != parseInt($scope.paginationConf.itemsPerPage))) {
                if ($scope.paginationConf.currentPage == 0) { $scope.paginationConf.currentPage = 1; }
                $scope.params.pageIndex = $scope.paginationConf.currentPage;
                $scope.params.pageSize = $scope.paginationConf.itemsPerPage;
                $scope.GetList();
            }
        }
    };

    //查询全部 或待批改
    $scope.correctgetFun = function () {
        $scope.params.pageIndex = "1";
        //查询单人最高成绩
        if ($scope.params.correct == 3 || $scope.params.correct == "3") {
            $scope.params.single = 2;
            //查询全部
        } else {
            $scope.params.single = 1;
        }
        $scope.GetList();
    }

    //排序
    $scope.OrderClick = function () {
        if ($scope.params.orderby == "desc") {
            $scope.params.orderby = "asc";
        } else {
            $scope.params.orderby = "desc";
        }
        $scope.GetList();

    }
//    //导出
//    $scope.exportExcel = function () {
//        $scope.excelurl = $rootScope.requestUrl + "/course/paperStatistics?paperid=" + $scope.params.paperid + "&orderby=" + $scope.params.orderby + "&searchText=" + $scope.params.searchText + "&correct=" + $scope.params.correct + "&sort=" + $scope.params.sort + "&export=2";
//        window.open($scope.excelurl);
//    }
    //导出
    $scope.exportExcel = function () {
        var paramExport = angular.copy($scope.params);
        paramExport.export = 2;
        RTS.paperStatistics(paramExport).then(function (response) {
            if (response.errorcode == "0" && response.errormsg != undefined && response.errormsg.length > 0) {
                window.location = response.errormsg;
            }
            else if (response.errorcode == "73" || (response.errorcode == "0" && (response.errormsg == undefined || response.errormsg.length <= 0))) {
                setTimeout(function () {
                    $scope.exportExcel();
                }, 3000);
                Msg.warning("正在统计请稍后..");
            } else {
                Msg.error("统计失败！");
            }
        }, function errorCallback() {
            console.log("连接服务器失败！");
        });

    }
    //查询试卷列表
    $scope.GetList = function () {
        $scope.loadingimg = "/images/chengji_loading.gif";
        ///images/chengji_loading.gif
        ///images/null.png
        if ($scope.isloading1) {
            Msg.warning("正在查询，请稍后!");
            return;
        }

        $scope.isloading1 = true;
        RTS.paperStatistics($scope.params).then(function (data) {
            $scope.isloading1 = false;
            if (data.errorcode == "0") {
                $scope.List = data.datas;
                if ($scope.List == undefined || $scope.List.length <= 0) {
                    $scope.loadingimg = "/images/null.png";
                    return;
                } else {
                    $scope.paginationConf.totalItems = data.numCount;
                    $scope.paginationConf.currentPage = data.pageIndex;
                }
            } else {
                $scope.loadingimg = "/images/null.png";
                Msg.error("请求数据有误");
            }
        }, function callback() {
            Msg.error("请求数据有误");
            $scope.loadingimg = "/images/null.png";
            $scope.isloading1 = false;
        });
    }


} ]);
